package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"hotel/model/mysql"

	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *hotelrpc.UserUpdateRequest) (*hotelrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var users mysql.User
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&users)
	if users.ID == 0 {
		return nil, fmt.Errorf("用户不存在,请重新输入")
	}

	user := mysql.User{
		Model:     gorm.Model{ID: uint(in.Id)},
		Username:  in.Username,
		Password:  in.Password,
		Image:     in.Image,
		Name:      in.Name,
		Sex:       in.Sex,
		Signature: in.Signature,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("用户不能修改")
	}

	return &hotelrpc.UserUpdateResponse{
		Id: int64(user.ID),
	}, nil
}
